package com.ruoyi.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.bizsys.domain.*;
import com.ruoyi.bizsys.service.ICustomerInfoService;
import com.ruoyi.business.domain.BusinessMarketUser;
import com.ruoyi.business.domain.CustomerVO;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusinessMapper;
import com.ruoyi.business.service.IBusinessService;
import org.springframework.util.CollectionUtils;

/**
 * @Author: Jamnsin
 * @Date: 2021/3/1 10:43 上午
 */
@Service
public class BusinessServiceImpl implements IBusinessService {

    @Autowired
    BusinessMapper businessMapper;

    @Autowired
    ICustomerInfoService customerInfoService;


    @Override
    public void initCust() {
        List<String> userIds = this.selectUserIdNoCust();
        if (CollectionUtils.isEmpty(userIds)) {
            return;
        }

        userIds.stream().forEach(p -> {
            CustomerInfo cust = new CustomerInfo();
            cust.setUserId(p);
            cust.setCustNo(IdUtils.generateCustNo());
            customerInfoService.insertCustomerInfo(cust);
        });
    }

    @Override
    public void bankUpLoanInfo(LoanIou loanIou) {
        businessMapper.bankUpLoanInfo(loanIou);
    }

    @Override
    public CustomerVO getCustomerInfo(String custId) {
        return businessMapper.getCustomerInfo(custId);
    }

    @Override
    public List<AppDeviceInfo> getDeviceOfUninitialized() {
        return businessMapper.getDeviceOfUninitialized();
    }

    @Override
    public int saveBatchDeviceFingerprints(List<AppDeviceFingerprint> objects) {
        return businessMapper.saveBatchDdeviceFingerprints(objects);
    }

    @Override
    public String getDeviceFirstVersion(String userUuid) {
        return businessMapper.getDeviceFirstVersion(userUuid);
    }

    @Override
    public Map getDeviceMapperMaps() {
        List<Map<String, String>> deviceMapperMaps = businessMapper.getDeviceMapperMaps();
        Map<String, String> hashMap = new HashMap<>();
        deviceMapperMaps.forEach(e -> {
            hashMap.put(e.get("phoneBrand"),e.get("mapperBrand"));
        });
        return hashMap;
    }

    @Override
    public String getDeviceMapper(String phoneBrand) {
        return businessMapper.getDeviceMapper(phoneBrand);
    }

    @Override
    public int upCreditRatingByCustNo(String creditRating, String customerNo) {
        if (Func.hasEmpty(creditRating,customerNo) || "-1".equals(creditRating)){
            return 0;
        }
        return businessMapper.upCreditRatingByCustNo(creditRating,customerNo);
    }

    /**
     * 营销用户信息修改
     * @param marketUser
     */
    @Override
    public void loanOrCreditUpdateMarketUserInfo(BusinessMarketUser marketUser) {
        businessMapper.loanOrCreditUpdateMarketUserInfo(marketUser);
    }


    @Override
    public void backUpCreditAccount(CreditAccount creditAccount) {
        businessMapper.backUpCreditAccount(creditAccount);
    }


    public List<String> selectUserIdNoCust() {
        return businessMapper.selectUserIdNoCust();
    }


}
